
import { isUpdateCustomerFromsessionStorage,isUpdateCustomerFromlocalStorage } from "@/api/customer"
const state = {
    customer: null,
    account: null
}

const mutations = {
    setCustomerAccount(state, obj) {
      
        state.account = obj.account
        state.customer = obj.customer
        window.sessionStorage.setItem('account', state.account? JSON.stringify(state.account) :null)
        window.sessionStorage.setItem('customer', state.customer ? JSON.stringify(state.customer) :null)
        
        if (window.localStorage.getItem('remembercustomer')) {
            window.localStorage.setItem('account', state.account? JSON.stringify(state.account) :null)
            window.localStorage.setItem('customer', state.account? JSON.stringify(state.account) :null)
        }else {
            window.localStorage.removeItem('account')
            window.localStorage.removeItem('customer')
        }
    },
    login(state,obj) {
        if(obj.remembercustomer){
            window.localStorage.setItem('remembercustomer',true)
        }
        this.commit('customer/setCustomerAccount', {
            customer: obj.customer,
            account: obj.account
        })
    },
    logout() {
       window.localStorage.removeItem('remembercustomer')
       this.commit('customer/setCustomerAccount', { })
    },
    refreshpage(state, obj) {
        const code = obj.res.code

        if (code === 220||code===320) {
            this.commit('customer/logout')
           
            alert(obj.res.msg)
        } else if (code === 210||code===310) {

            this.commit('customer/setCustomerAccount', {
                customer:obj.res.data.customer,
                account: obj.res.data.account
            })
            alert(obj.res.msg)
        } else if(code===300){
            this.commit('customer/setCustomerAccount', {
                customer: obj.customer,
                account: obj.res.data.account
            })
        }else if(code===200){
            this.commit('customer/setCustomerAccount', {
                customer: obj.customer,
                account: obj.account
            })
        }
    }
}
const actions = {
    /**
     * 初始化和刷新页面时 判断用户是否登录并挂载到vuex中
     */
    refreshpage(context) {
        if (window.sessionStorage.getItem('customer') && window.sessionStorage.getItem('account')) {
            const customer = JSON.parse(window.sessionStorage.getItem('customer'))
            const account = JSON.parse(window.sessionStorage.getItem('account'))
            //向后台发送请求 查看用户信息是否被修改过且是否是重大修改
            isUpdateCustomerFromsessionStorage().then(res => {
                // console.log(res)
                context.commit('refreshpage', { customer, account, res })

            })
            
        }else{
        if(window.localStorage.getItem('remembercustomer')&&window.localStorage.getItem('customer')&&window.localStorage.getItem('account')){
            const customer = JSON.parse(window.localStorage.getItem('customer'))
            const account = JSON.parse(window.localStorage.getItem('account'))
            isUpdateCustomerFromlocalStorage(customer,account).then(res=>{
                // console.log(res)
                        context.commit('refreshpage', { customer, account, res })
            })
        }
    }
}
}


const getters = {
    isLogin(state){
        return state.customer&&state.account
    }
}
export default {
    namespaced: true,
    state,
    actions,
    getters,
    mutations
}